package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.product.service.DynamicService;
import com.wing.sell.dao.CustomerDueDao;
import com.wing.sell.model.entity.CustomerDue;
import com.wing.sell.model.request.CustomerDueForm;
import com.wing.sell.model.response.CustomerDueVO;
import com.wing.sell.service.CustomerDueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 供应商尽调记录表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CustomerDueServiceImpl extends ServiceImpl<CustomerDueDao, CustomerDue> implements CustomerDueService {

    @Autowired
    private DynamicService dynamicService;


    @Override
    @Transactional
    public void add(CustomerDueForm customerDueForm) throws  Exception{

        //校验

        CustomerDue customerDue =new CustomerDue();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(CustomerDue.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(customerDueForm, customerDue, copyOptions);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        String code=dynamicService.getDayNextCode("sel_customer_due");
        String day=simpleDateFormat.format(new Date());
        customerDue.setCode("D"+day+code);
        customerDue.setDay(Integer.valueOf(day));
        customerDue.setNo(Integer.valueOf(code));

        customerDue.setStatus(CustomerDue.Status.oncheck);
        baseMapper.insert(customerDue);
    }




    @Override
    public List<CustomerDueVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<CustomerDueVO> pageList4app(Page page, Map<String, Object> params) {
        List<CustomerDueVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public CustomerDueVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public void update(CustomerDueForm customerDueForm) throws Exception {
        CustomerDue customerDue = this.getById(customerDueForm.getId());
        if (customerDue==null){
            throw new BusinessException("修改失败,未找到尽调记录！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(CustomerDue.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(customerDueForm, customerDue, copyOptions);
        this.updateById(customerDue);
    }

}
